﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VideoClassLibrary.BusinessLogicLayer;
using VideoClassLibrary.Extension;
using VideoClassLibrary.Model;

public partial class UserManagement_QuantityRegisterUser : AdminUserBase
{
    public List<V_user> UserList = new List<V_user>();
    public int CurrentTraingClassId = 0;
    //public int CurrentTraingClassId
    //{
    //    get
    //    {
    //        if (ViewState["view_CurrentTraingClassId"] == null) ViewState["view_CurrentTraingClassId"] = 0;
    //        return (int)ViewState["view_CurrentTraingClassId"];
    //    }
    //    set { ViewState["view_CurrentTraingClassId"] = value; }
    //}

    public string CurrentTraingClassName = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        SetAutoCompleteContextKey();
        if (!string.IsNullOrEmpty(Request.QueryString["TClassId"]))
        {
            CurrentTraingClassId = int.Parse(Request.QueryString["TClassId"].ToString());
            CurrentTraingClassName = Request.QueryString["TClassName"].ToString();
        }
        if (!this.IsPostBack)
        {
                SetPageData();        
        }
    }

    private void SetPageData()
    {
        //HiddenField hidTrainingClassId = null;
        //TextBox txtTrainingClassName = null;
        //for (int i = 1; i <= 10; i++)
        //{
            //hidTrainingClassId = (HiddenField)this.FindControl("hidTrainingClassId" + i);
            //txtTrainingClassName = (TextBox)this.FindControl("txtTrainingClassName" + i);
            //hidTrainingClassId.Value = CurrentTraingClassId.ToString();
            //txtTrainingClassName.Text = CurrentTraingClassName;
            //txtTrainingClassName.Enabled = false;
        //}
        TrainClassModule TCM = new TrainClassModule();
        V_TrainClass model = TCM.GetTrainClassModelById(CurrentTraingClassId);
        lblMaxUserCount.Text = model.MaxUserCount.ToString();
        lblVTC_name.Text = model.VTC_name;
        lblVTC_Number.Text = model.VTC_Number;
        lblVTC_starDate.Text = model.VTC_StartDate.ToString("yyyy-MM-dd");
        lblVTC_endDate.Text = model.VTC_EndDate.ToString("yyyy-MM-dd");
        //lblVTC_desc.Text = model.VTC_desc;
        divVTC_desc.InnerHtml = model.VTC_desc;

    }


    #region WebMethod
    [System.Web.Services.WebMethod]
    [System.Web.Script.Services.ScriptMethod]
    public  static string[] GetUserInfoByRealName(string prefixText, int count, string contextKey)
    {
        try
        {
            string[] result = null;
            UserModule userHelper = new UserModule();
            DataSet ds = userHelper.GetUserListByWhere(" and VU_realname  like '" + prefixText + "%'", int.Parse(contextKey));
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                result = new string[ds.Tables[0].Rows.Count];
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr=ds.Tables[0].Rows[i];
                    string hidData = dr["ID"].ToString() + "|" + dr["VU_loginname"].ToString() + "|" + dr["VU_mobile"].ToString() + "|" + dr["VU_email"].ToString();//格式如下：ID|登录名|电话|email
                    result[i] = "{'First':'" + dr["VU_realname"].ToString() + " " + dr["VU_loginname"].ToString() + "','Second':'" + hidData+ "'}"; 

                }
            }
            return result;
        }
        catch
        {
            // TODO: logging...
            return null;
        }
    }
    //[System.Web.Services.WebMethod]
    //[System.Web.Script.Services.ScriptMethod]
    //public static string[] GetTrainingClass(string prefixText, int count, string contextKey)
    //{
    //    try
    //    {
    //        string[] result = null;
    //        TrainClassModule trainingModule = new TrainClassModule();
    //        DataSet ds = trainingModule.GetTrainclassListByWhere(int.Parse(contextKey), "  and LOWER(VTC_name) like '" + prefixText.ToLower() + "%'");
    //        if (ds != null && ds.Tables[0].Rows.Count > 0)
    //        {
    //            result = new string[ds.Tables[0].Rows.Count];
    //            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    //            {
    //                DataRow dr = ds.Tables[0].Rows[i];
    //                result[i] = "{'First':'" + dr["VTC_name"].ToString() + "','Second':" + dr["ID"].ToString() + "}";

    //            }
    //        }
    //        return result;
    //    }
    //    catch
    //    {
    //        // TODO: logging...
    //        return null;
    //    }
    //}
    #endregion

    private void SetAutoCompleteContextKey()
    {
        for (int i = 1; i <= 10; i++)
        {
            AjaxControlToolkit.AutoCompleteExtender AceUserRealName = (AjaxControlToolkit.AutoCompleteExtender)this.FindControl("AceUserRealName" + i);
            //AjaxControlToolkit.AutoCompleteExtender AceTrainingClass = (AjaxControlToolkit.AutoCompleteExtender)this.FindControl("AceTrainingClass" + i);
            AceUserRealName.ContextKey = this.SchoolID.ToString();
            //AceTrainingClass.ContextKey = this.SchoolID.ToString();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        TextBox txtLoginName = null;
        TextBox txtRealName = null;
        TextBox txtMobile = null;
        TextBox txtEmail = null;
        DropDownList ddlIsBuyingAccount = null;

        HiddenField hidUserId = null;
        //HiddenField hidTrainingClassId = null;
        int failCount = 0;
        int successCount = 0;
        for (int i = 1; i <= 10; i++)
        {
             txtLoginName = (TextBox)this.FindControl("txtLoginName" + i);
             txtRealName = (TextBox)this.FindControl("txtUserRealName" + i);
             txtMobile = (TextBox)this.FindControl("txtMobile" + i);
             txtEmail = (TextBox)this.FindControl("txtEmail" + i);
             ddlIsBuyingAccount = (DropDownList)this.FindControl("ddlIsBuyingAccount" + i);
             if (txtLoginName.Text.Trim() == "" && txtRealName.Text.Trim()=="" && txtMobile.Text.Trim()=="" && txtEmail.Text.Trim()=="")
             {
                 UserList.Add(null);
                 continue;
             }

             hidUserId = (HiddenField)this.FindControl("hidUserId" + i);
             //hidTrainingClassId = (HiddenField)this.FindControl("hidTrainingClassId" + i);

             CreateUserList(hidUserId.Value, txtLoginName.Text.Trim(), txtRealName.Text.Trim(),
             txtEmail.Text.Trim(), txtMobile.Text.Trim());
            

        }
        UserModule userHelper = new UserModule();
        List<ReturnResult> resultList=  userHelper.QuantityRegisterUser(UserList, this.SchoolID);
        ReturnResult result = null;
        for (int i = 0; i < resultList.Count; i++)
        {
            result = resultList[i];
            Label lblMsg = (Label)this.FindControl("lblMsg"+(i+1));
            if (result.IsSuccess == true)
            {
                successCount += 1;
                lblMsg.Text = "成功!";
            }
            else if (result.ErrorMsg != "")
            {
                lblMsg.Text = result.ErrorMsg;
                failCount += 1;
            }
            else
            {

            }
        }
        if (successCount > 0 && failCount == 0)
        {
            JsHelper.AlertAndRedirect("添加成功！","ViewTrainingClassList.aspx");
        }
    }

    private void CreateUserList(string userId, string txtLoginName, string txtRealName, string txtEmail, string txtMobile)
    {
        V_user user = new V_user();
        user.ID = int.Parse(userId);
        user.VU_loginname = txtLoginName;
        user.VU_Password = txtLoginName;
        user.VU_realname = txtRealName;
        user.VU_mobile = txtMobile;
        user.VU_email = txtEmail;
        //user.IsAvailable = IsAvailable;
        string [] trainingClassIdList=new string[1];
        trainingClassIdList[0] = CurrentTraingClassId.ToString();
       // user.TrainingClassIdList = trainingClassIdList;
        UserList.Add(user);
      
    }
    protected void btnRedirectList_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewUserInfoList.aspx");
    }
}
